package com.buka.role.controller;

import com.buka.common.ResultJson;
import com.buka.role.entity.Role;
import com.buka.role.service.RoleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/role/add")
public class AddServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String name = req.getParameter("name");
        String code = req.getParameter("code");

        RoleService roleService = new RoleService();
        int add = roleService.add(new Role(name, code));

        //不论成功或者失败，返回json字符串，而不是跳转页面
        //定义一个全局响应
        if(add >0 ){
            resp.getWriter().write(ResultJson.success());
        }else {
            resp.getWriter().write(ResultJson.error());
        }
    }
}
